import { Injectable } from '@angular/core';
import { BaseService } from '@core/routes-services/base.service';
import { CacheService } from '@core/routes-services/cache.service';
import { HttpClient } from '@angular/common/http';
import { myValidNull } from '@core/util/string-utils';

/**
 * 粉丝 服务
 */
@Injectable()
export class MarketFansService extends BaseService {

  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
  }

  base_url: any = 'api/market/customers';


  /**
   * 分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  pagination(page: any, keyword: any, salesmanId: any, sortName: any, sortValue: any): Promise<any> {
    const url = this.base_url + '/pagination';
    if (keyword) {
      page['_filter_like_keyword'] = keyword;
    }
    if (salesmanId) {
      page['_filter_eq_salesmanId'] = salesmanId;
    }
    if (myValidNull(sortName) || myValidNull(sortValue)) {
      page['_sort_by_'] = 'lastPayTime_desc';
    } else {
      page['_sort_by_'] = sortName + '_' + (sortValue == 'ascend' ? 'asc' : 'desc');
    }
    return this.post(url, page);
  }

  /**
   * 我的团队分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  teamPagination(page: any, keyword: any, salesmanId: any): Promise<any> {
    const url = this.base_url + `/${salesmanId}/team/pagination`;
    if (keyword) {
      page['_filter_like_keyword'] = keyword;
    }
    return this.post(url, page);
  }

  /**
   * 搜索下拉框
   *
   * @param user
   * @returns {Promise<any>}
   */
  searchOptions(keyword: any): Promise<any> {
    const url = this.base_url + '/pagination';
    let page: any = { number: 1, size: 15, totalElements: 0, content: [] };
    if (keyword) {
      page['_filter_like_name'] = keyword;
    }
    page['_sort_by_'] = 'id_desc';
    return this.post(url, page);
  }

  /**
   * 分页
   *
   * @param triggerId
   * @returns {Promise<any>}
   */
  getByIds(triggerId: any): Promise<any> {
    const url = this.base_url + '/actions/customers/' + triggerId;
    return this.post(url, {});
  }

  /**
   * 获取查看详情
   *
   * @param id
   * @returns {Promise<any>}
   */
  detail(id: any): Promise<any> {
    const url = this.base_url + '/' + id;
    return this.get(url);
  }


  /**
   * 查看关系链
   * @param id
   * @param page
   * @returns {Promise<any>}
   */
  relatesChain(id: any, page: any): Promise<any> {
    const url = this.base_url + '/relates_chain/' + id;
    return this.post(url, page);
  }

  /**
   * 修改
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  updateEntity(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/' + id;
    return this.put(url, entity);
  }

  /**
   * 根据用户ID 获取相关信息构造（获取） saleman对象
   * @param id
   * @returns {Promise<any>}
   */
  generateSalesman(id: any): Promise<any> {
    const url = this.base_url + '/actions/generateSalesman/' + id;
    return this.get(url);
  }

  /**
   * 修改上级
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  changeParent(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/actions/' + id + '/change/parent';
    return this.put(url, entity);
  }

  /**
   * 动态管理员设置
   *
   * @param id
   * @param beAuth
   * @returns {Promise<any>}
   */
  setAuth(id: any, beAuth: any): Promise<any> {
    const url = this.base_url + '/actions/auth/' + id + '/' + beAuth;
    return this.put(url, {});
  }
}
